using UnityEngine;
using UnityEngine.UI;
using TMPro;
using RPG.Domain.Character;
using RPG.Application.Services.Character;
using RPG.Application.Commands.Character;
using RPG.Presentation.Core;

namespace RPG.Presentation.Character
{
    public class CharacterPresenter : MonoBehaviour
    {
        [SerializeField] private TextMeshProUGUI nameText;
        [SerializeField] private TextMeshProUGUI levelText;
        [SerializeField] private TextMeshProUGUI statsText;
        [SerializeField] private Button levelUpButton;

        private ICharacterService _characterService;
        private CharacterId _currentCharacterId;

        private void Start()
        {
            _characterService = GameManager.Instance.GetCharacterService();
            levelUpButton.onClick.AddListener(OnLevelUpButtonClicked);
            CreateInitialCharacter();
        }

        private async void CreateInitialCharacter()
        {
            var command = new CreateCharacterCommand("Hero");
            var character = await _characterService.CreateCharacterAsync(command);
            _currentCharacterId = new CharacterId(character.Id);
            UpdateUI(character);
        }

        private async void OnLevelUpButtonClicked()
        {
            if (_currentCharacterId == null) return;

            var command = new LevelUpCharacterCommand(_currentCharacterId);
            var character = await _characterService.LevelUpCharacterAsync(command);
            UpdateUI(character);
        }

        private void UpdateUI(CharacterDTO character)
        {
            if (character == null) return;

            nameText.text = $"Name: {character.Name}";
            levelText.text = $"Level: {character.Level}";
            statsText.text = $"Stats:\n" +
                           $"STR: {character.Stats.Strength}\n" +
                           $"AGI: {character.Stats.Agility}\n" +
                           $"INT: {character.Stats.Intelligence}\n" +
                           $"HP: {character.Stats.Health}\n" +
                           $"MP: {character.Stats.Mana}";
        }
    }
}
